package com.cqw.common;

import com.cqw.utils.StringUtils;
import com.github.pagehelper.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-29 10:43
 * @Description:
 */
@Data
public class BaseResponse extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 响应码
     */
    private static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    private static final String MSG_TAG = "message";

    /**
     * 对象数据
     */
    private static final String DATA_TAG = "data";

    /**
     * 分页信息
     */
    private static final String PAGINATION = "pagination";


    /**
     * 分页信息
     *//*
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Pagination pagination;*/


    /**
     * 初始化一个BaseResponse对象，使其表示一个空消息
     */
    public BaseResponse() {

    }

    /**
     * 初始化一个BaseResponse对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public BaseResponse(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }


    /**
     * 初始化一个BaseResponse对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public BaseResponse(int code, String msg, Object data, Pagination pagination) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data)) {
            super.put(DATA_TAG, data);
        }
        if (StringUtils.isNotNull(pagination)) {
            super.put(PAGINATION, pagination);
        }
    }


    /**
     * 方便链式调用
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    public BaseResponse put(String key, Object value) {
        super.put(key, value);
        return this;
    }


    /**
     * 返回成功的消息
     *
     * @return
     */
    public static BaseResponse success() {
        return BaseResponse.success("操作成功");
    }


    /**
     * 返回成功的消息
     *
     * @return
     */
    public static BaseResponse success(Object data) {
        return BaseResponse.success("操作成功", data);
    }


    /**
     * 返回成功的消息
     *
     * @return
     */
    /*public static BaseResponse success(String msg) {
        return BaseResponse.success(msg, null);
    }*/


    /**
     * 返回成功的消息
     *
     * @return
     */
    public static BaseResponse success(String msg, Object data) {
        if (data instanceof Page) {
            Page page = (Page) data;
            Pagination pagination = null;

            int totalRow = (int) page.getTotal();
            int pageSize = (int) page.getPageSize();
            int pageIndex = (int) page.getPageNum();
            if (totalRow > 0 || pageSize > 0 || pageIndex > 0) {
                pagination = new Pagination(totalRow, pageSize, pageIndex);
            }
            return new BaseResponse(HttpStatus.SUCCESS, msg, data, pagination);

        }
        return new BaseResponse(HttpStatus.SUCCESS, msg, data, null);
    }


    /**
     * 返回错误的消息
     *
     * @return
     */
    public static BaseResponse error() {
        return BaseResponse.error("操作失败");
    }


    /**
     * 返回错误的消息
     *
     * @return
     */
    public static BaseResponse error(String msg) {
        return BaseResponse.error(msg, null);
    }

    /**
     * 返回错误的消息
     *
     * @return
     */
    public static BaseResponse error(String msg, Object data) {
        return new BaseResponse(HttpStatus.ERROR, msg, data, null);
    }


    /**
     * 返回错误的消息
     *
     * @return
     */
    public static BaseResponse error(int code, String msg) {
        return new BaseResponse(code, msg, null, null);
    }


}
